import { publicApi } from "@/utils/request/api/public";
import { mineApi } from "@/utils/request/api/mine";
import Toast from '@vant/weapp/toast/toast';
const { imgUrl } = getApp().globalData;
Page({
  data: {
    imgUrl: imgUrl + "mine/",
    from: "",
    origin_auth: {},
    authority: {
      real_name: "",
      // gender: true,
      job: "", //单位
      position: "", //职务
      job_type: "", //单位类型
      bank: "", //开户行
      bank_no: "", //银行账号
      good_at: "", //擅长
      address: "", //常住地
      avoid_unit: "", //避开单位
      idcard_front_side_url: "", //身份证正面照
      idcard_back_side_url: "", //身份证背面照
      title_certificate_url: "", //职称证书
      expert_appointment_url: "", //专家聘书
      other_file_list: [], //其他文件
      avoid_unit_list: [], //避开单位
    },
    industryList: [],
    banks: ["工商银行", "建设银行", "招商银行", "农业银行"],
  },
  // 立即认证
  im_auth() {
    if (this.data.from == 'edit') {
      if (JSON.stringify(this.data.authority) == JSON.stringify(this.data.origin_auth)) {
        Toast.fail('没有修改任何内容！');
        return;
      }
    }
    let obj = JSON.parse(JSON.stringify(this.data.authority))
    if (!obj.real_name.trim()) {
      Toast.fail('请输入姓名！');
      return;
    }
    if (!obj.job.trim()) {
      Toast.fail('请输入工作单位！');
      return;
    }
    if (!obj.position.trim()) {
      Toast.fail('请输入职务！');
      return;
    }
    if (!obj.job_type.trim()) {
      Toast.fail('请输入单位类型！');
      return;
    }
    if (!obj.bank.trim()) {
      Toast.fail('请输入开户行！');
      return;
    }
    if (!obj.bank_no.trim()) {
      Toast.fail('请输入银行账号！');
      return;
    }
    if (!obj.good_at.trim()) {
      Toast.fail('请输入擅长领域！');
      return;
    }
    if (!obj.address.trim()) {
      Toast.fail('请输入常住地！');
      return;
    }
    if (!obj.title_certificate_url.trim()) {
      Toast.fail('请上传职称证书！');
      return;
    }
    if (!obj.expert_appointment_url.trim()) {
      Toast.fail('请上传专家聘书！');
      return;
    }
    if (obj.avoid_unit) {
      obj.avoid_unit_list.unshift(obj.avoid_unit)
    }
    mineApi.editAuthenZj(obj).then(res => {
      if(res.err_code == 0) {
        Toast.success(res.err_msg);
        wx.navigateBack();
      } else {
        Toast.fail(res.err_msg);
        wx.navigateBack();
      }
    }).catch(err => {
      Toast.fail('认证失败，请稍后再试！');
    })
  },
  addAvoidUnit() {
    if (!this.data.authority.avoid_unit.trim()) {
      Toast.fail('请输入回避单位！');
      return;
    }
    let obj = JSON.parse(JSON.stringify(this.data.authority))
    obj.avoid_unit_list.unshift(obj.avoid_unit)
    obj.avoid_unit = ''
    this.setData({
      authority: obj
    })
  },
  subAvoidUnit(event) {
    const {currentTarget: {dataset: {index}}} = event
    let obj = JSON.parse(JSON.stringify(this.data.authority))
    obj.avoid_unit_list.splice(index, 1)
    this.setData({
      authority: obj
    })
  },
  change(event) {
    const {
      currentTarget: {
        dataset: { type },
      },
    } = event;
    let authority = JSON.parse(JSON.stringify(this.data.authority));
    authority[type] = event.detail.value;
    this.setData({
      authority,
    });
  },
  select(event) {
    const {
      currentTarget: {
        dataset: { type, origin },
      },
    } = event;
    let authority = JSON.parse(JSON.stringify(this.data.authority));
    authority[type] = this.data[origin][event.detail.value / 1];
    this.setData({
      authority,
    });
  },
  chooseImage(event) {
    const that = this;
    const {
      currentTarget: {
        dataset: { sfz },
      },
    } = event;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function (res) {
        const filePath = res.tempFilePaths[0];
        const formData = {
          fileVal: filePath,
        };

        publicApi
          .uploadFile({
            filePath,
            formData,
          })
          .then((res) => {
            let obj = JSON.parse(JSON.stringify(that.data.authority))
            if (sfz == 0) {
              obj.idcard_front_side_url = res.data
            } else if (sfz == 1) {
              obj.idcard_back_side_url = res.data
            } else if (sfz == 2) {
              obj.title_certificate_url = res.data
            } else if (sfz == 3) {
              obj.expert_appointment_url = res.data
            } else if (sfz == 4) {
              obj.other_file_list.push(res.data)
            }
            that.setData({
              authority: obj
            })
          });
      },
    });
  },
  afterRead() {},
  onLoad(options) {
    publicApi
      .getIndustryList()
      .then((res) => {
        this.setData({
          industryList: res.data.map((item) => item.name),
        });
      })
      .catch((err) => {
        Toast.fail("获取信息失败，请稍后再试！");
      });
    const from = options.from;
    if (from != "edit") {
      return ;
    }
    this.setData({
      from: from
    })
    mineApi
     .getAuthenZj()
     .then((res) => {
        console.log("res in list get auth === ", res);
        let obj = res.data;
        obj.other_file_list = obj.other_file_list.split(',') || [];
        obj.avoid_unit_list = obj.avoid_unit_list.split(',') || [];
        this.setData({
          authority: obj,
          origin_auth: obj
        });
      })
     .catch((err) => {
        Toast.fail("获取信息失败，请稍后再试！");
      });
  },
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onShareAppMessage() {
    return {
      title: "",
    };
  },
});
